package com.league.zjxn.main.mine

import android.content.Context
import androidx.core.widget.addTextChangedListener
import androidx.lifecycle.LifecycleOwner
import com.bigkoo.pickerview.builder.OptionsPickerBuilder
import com.bigkoo.pickerview.listener.OnOptionsSelectListener
import com.bigkoo.pickerview.view.OptionsPickerView
import com.blankj.utilcode.util.ActivityUtils
import com.blankj.utilcode.util.GsonUtils
import com.blankj.utilcode.util.LogUtils
import com.league.zjxn.R
import com.league.zjxn.entity.IndustryList
import com.league.zjxn.jetpack.vmodel.UserViewModel
import com.league.zjxn.main.home.AgreeDetailsActivity
import com.league.zjxn.main.popup.LoadingDialog
import com.module.lemlin.http.HttpResponseBody
import com.module.lemlin.http.Status
import com.module.lemlin.http.msg
import com.module.lemlin.owner.OwnerViewModeActivity
import kotlinx.android.synthetic.main.activity_league_approve.*
import kotlinx.android.synthetic.main.activity_league_notice.*


class LeagueApproveActivity : OwnerViewModeActivity<UserViewModel>() {

    companion object {
        fun open() {
            ActivityUtils.startActivity(LeagueApproveActivity::class.java)
        }
    }

    private var mIndustryId: String? = null
    private val mOptionsIndustry: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this as Context) { options1, option2, options3, v ->
            val industryList = viewModel.industryListLiveData.value
                ?.data?.data ?: return@OptionsPickerBuilder
            mIndustryId = industryList[options1].id
            tvLeagueApproveIndustry.text = industryList[options1].name
        }.build()
    }

    private var mProvinceId: String? = null
    private var mCityId: String? = null
    private val mOptionsRegion: OptionsPickerView<String> by lazy {
        OptionsPickerBuilder(this as Context) { options1, option2, options3, v ->
            val regionList = viewModel.regionListLiveData.value
                ?.data?.data ?: return@OptionsPickerBuilder
            val province = regionList[options1]
            mProvinceId = province.id
            val city = province.children
            mCityId = if (city.isNullOrEmpty())
                null else city[option2].id
            tvLeagueApproveProvince.text = "${province.name}\t${
                if (city.isNullOrEmpty())
                    "" else city[option2].name
            }"
        }.build()
    }

    override fun viewModel(): UserViewModel {
        return createViewModel()
    }

    override val layoutResId: Int
        get() = R.layout.activity_league_approve

    override fun stateBar(): StatusBar {
        return StatusBar(view = flLeagueApproveBar)
    }

    override fun initView() {

        //地区选择
        tvLeagueApproveProvince.setOnClickListener {
            viewModel.userRegionList()
        }
        //企业行业
        tvLeagueApproveIndustry.setOnClickListener {
            viewModel.userIndustryList()
        }

        etLeagueApproveTitle.addTextChangedListener {
            tvLeagueApproveSubmit.isEnabled =
                !it.isNullOrEmpty() && !etLeagueApproveContact.text.isNullOrEmpty()
                        && !etLeagueApproveMobile.text.isNullOrEmpty()
                        && !tvLeagueApproveIndustry.text.isNullOrEmpty()
                        && !etLeagueApproveAppeal.text.isNullOrEmpty()
        }
        etLeagueApproveContact.addTextChangedListener {
            tvLeagueApproveSubmit.isEnabled =
                !it.isNullOrEmpty() && !etLeagueApproveTitle.text.isNullOrEmpty()
                        && !etLeagueApproveMobile.text.isNullOrEmpty()
                        && !tvLeagueApproveIndustry.text.isNullOrEmpty()
                        && !etLeagueApproveAppeal.text.isNullOrEmpty()
        }
        etLeagueApproveMobile.addTextChangedListener {
            tvLeagueApproveSubmit.isEnabled =
                !it.isNullOrEmpty() && !etLeagueApproveTitle.text.isNullOrEmpty()
                        && !etLeagueApproveContact.text.isNullOrEmpty()
                        && !tvLeagueApproveIndustry.text.isNullOrEmpty()
                        && !etLeagueApproveAppeal.text.isNullOrEmpty()
        }
//        tvLeagueApproveProvince.addTextChangedListener {
//            tvLeagueApproveSubmit.isEnabled =
//                !it.isNullOrEmpty() && !etLeagueApproveTitle.text.isNullOrEmpty()
//                        && !etLeagueApproveContact.text.isNullOrEmpty()
//                        && !etLeagueApproveMobile.text.isNullOrEmpty()
//                        && !tvLeagueApproveIndustry.text.isNullOrEmpty()
//                        && !etLeagueApproveAppeal.text.isNullOrEmpty()
//        }
        tvLeagueApproveIndustry.addTextChangedListener {
            tvLeagueApproveSubmit.isEnabled =
                !it.isNullOrEmpty() && !etLeagueApproveTitle.text.isNullOrEmpty()
                        && !etLeagueApproveContact.text.isNullOrEmpty()
                        && !etLeagueApproveMobile.text.isNullOrEmpty()
                        && !etLeagueApproveAppeal.text.isNullOrEmpty()
        }
        etLeagueApproveAppeal.addTextChangedListener {
            tvLeagueApproveSubmit.isEnabled =
                !it.isNullOrEmpty() && !etLeagueApproveTitle.text.isNullOrEmpty()
                        && !etLeagueApproveContact.text.isNullOrEmpty()
                        && !etLeagueApproveMobile.text.isNullOrEmpty()
                        && !tvLeagueApproveIndustry.text.isNullOrEmpty()
        }


        tvLeagueApproveSubmit.setOnClickListener {
            if (cbLeagueApproveSelect.isChecked) {
                viewModel.userLeagueApprove(
                    name = etLeagueApproveTitle.text.toString(),
                    person = etLeagueApproveContact.text.toString(),
                    contract = etLeagueApproveMobile.text.toString(),
                    industry_id = mIndustryId,
                    province_id = mProvinceId,
                    city_id = mCityId,
                    content = etLeagueApproveAppeal.text.toString(),
                )
            } else {
                toast("请阅读并同意《申请加入合作协议》")
            }
        }

        tvLeagueApproveProtocol.setOnClickListener {
            AgreeDetailsActivity.openProtocol("合作协议")
        }
    }

    override fun initViewMode() {
        viewModel.leagueApproveLiveData.observe(this as LifecycleOwner, { response ->
            responseLoadingDialog(response)
            if (response.status != Status.SUCCESS)
                return@observe
            LeagueSuccessActivity.open()
        })

        viewModel.industryListLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            mOptionsIndustry.setPicker(data.map { it.name })
            mOptionsIndustry.show()
        })

        viewModel.regionListLiveData.observe(this as LifecycleOwner, { response ->
            if (response.status != Status.SUCCESS)
                return@observe
            val data = response.data?.data
                ?: return@observe
            val provinceList = data.map { it.name }

            val cityList: MutableList<MutableList<String>> = mutableListOf()
            data.forEach { province ->
                if (province.children.isNullOrEmpty()) {
                    cityList.add(mutableListOf())
                } else {
                    cityList.add(province.children.map {
                        it.name
                    }.toMutableList())
                }
            }

            mOptionsRegion.setPicker(provinceList, cityList)
            mOptionsRegion.show()
        })
    }

    private fun responseLoadingDialog(response: HttpResponseBody<*>) {
        when (response.status) {
            Status.START -> LoadingDialog.show(
                this as Context, resId = R.layout.dialog_load_style_1
            )
            Status.FAILURE -> {
                LoadingDialog.dismiss()
                toast(response.error?.msg)
            }
            Status.SUCCESS -> {
                LoadingDialog.dismiss()
            }
        }
    }
}